package com.vol.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vol.RedisUtils;
import com.vol.dao.SysMenuDao;
import com.vol.entity.SysMenu;
import com.vol.entity.UserInfo;
import com.vol.mapper.SysMenuMapper;
import com.vol.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuService {
    @Autowired
    private UserContext userContext;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private RedisUtils<List<Map<String, Object>>> redisUtils;


    public Map<String, Object> getTreeItem(int menuId) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<SysMenu> qw = new LambdaQueryWrapper<>();
        qw.eq(SysMenu::getMenuId, menuId);
        SysMenu sysMenu = sysMenuMapper.selectOne(qw);
        if (sysMenu == null) {
            sysMenu = new SysMenu();
        }
        map.put("menu_Id", sysMenu.getMenuId());
        map.put("parentId", sysMenu.getParentid());
        map.put("menuName", sysMenu.getMenuname());
        map.put("url", sysMenu.getUrl());
        map.put("auth", sysMenu.getAuth());
        map.put("orderNo", sysMenu.getOrderno());
        map.put("icon", sysMenu.getIcon());
        map.put("enable", sysMenu.getEnable());
        map.put("menuType", sysMenu.getMenutype() == null ? 0 : sysMenu.getMenutype());
        map.put("createDate", sysMenu.getCreatedate());
        map.put("creator", sysMenu.getCreator());
        map.put("tableName", sysMenu.getTablename());
        map.put("modifyDate", sysMenu.getModifydate());
        return map;
    }

    public List<Map<String, Object>> getMeunConsoleMap() {
        UserInfo userInfo = userContext.getUserInfo();
        int RoleId = userInfo.getRole_Id();

        List<SysMenu> list = null;
        if (RoleId == 1) {//管理员角色
            LambdaQueryWrapper<SysMenu> qw = new LambdaQueryWrapper<>();
            qw.orderByDesc(SysMenu::getOrderno, SysMenu::getParentid);
            list = sysMenuMapper.selectList(qw);
        } else {//其他角色
            list = sysMenuMapper.selectAllMenuAuth(RoleId);
        }
        List<Map<String, Object>> listmap = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", list.get(i).getMenuId());
            map.put("parentId", list.get(i).getParentid());
            map.put("name", list.get(i).getMenuname());
            map.put("MenuType", list.get(i).getMenutype());
            map.put("OrderNo", list.get(i).getOrderno());
            listmap.add(map);
        }
        return listmap;
    }

    public List<Map<String, Object>> getMenuMap() throws JsonProcessingException {
        UserInfo userInfo = userContext.getUserInfo();
        int RoleId = userInfo.getRole_Id();

        List<SysMenu> list = null;
        if (RoleId == 1) {//管理员角色
            LambdaQueryWrapper<SysMenu> qw = new LambdaQueryWrapper<>();
            qw.orderByDesc(SysMenu::getOrderno, SysMenu::getParentid);
            list = sysMenuMapper.selectList(qw);
        } else {//其他角色
            list = sysMenuMapper.selectAllMenuAuth(RoleId);
        }
        List<Map<String, Object>> listmap = null;
        if (redisUtils.getKey("meunCache" + userInfo.getUser_Id()) == null) {
            listmap = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                String Auth = list.get(i).getAuth();
                ObjectMapper objectMapper = new ObjectMapper();
                Map<String, String>[] Actions = objectMapper.readValue(Auth, Map[].class);
                List<String> valueList = new ArrayList<>();
                for (int j = 0; j < Actions.length; j++) {
                    valueList.add(Actions[j].get("value"));
                }

                Map<String, Object> map = new HashMap<>();
                map.put("id", list.get(i).getMenuId());
                map.put("name", list.get(i).getMenuname());
                map.put("url", list.get(i).getUrl());
                map.put("parentId", list.get(i).getParentid());
                map.put("icon", list.get(i).getIcon());
                map.put("enable", list.get(i).getEnable());
                map.put("tableName", list.get(i).getTablename());
                map.put("permission", valueList);
                listmap.add(map);
            }
            redisUtils.setKey("meunCache" + userInfo.getUser_Id(), listmap);
        } else {
            listmap = redisUtils.getKey("meunCache" + userInfo.getUser_Id());
        }
        return listmap;
    }

    public void save(SysMenu sysMenu) throws JsonProcessingException {

        UserInfo userInfo = userContext.getUserInfo();
        if (sysMenu.getMenuId() == 0) {
            sysMenu.setCreator(userInfo.getUserName());
            sysMenu.setCreatedate(new Date());
            int mun = sysMenuMapper.insert(sysMenu);
            System.out.println(mun + "");
        } else {
            sysMenu.setModifier(userInfo.getUserName());
            sysMenu.setModifydate(new Date());
            sysMenuMapper.updateById(sysMenu);
        }
        reloadCache();
    }

    public void del(Integer menuId) throws JsonProcessingException {
        sysMenuMapper.deleteById(menuId);
        reloadCache();
    }

    public void reloadCache() throws JsonProcessingException {
        UserInfo userInfo = userContext.getUserInfo();
        int RoleId = userInfo.getRole_Id();
        List<Map<String, Object>> listmap = new ArrayList<>();
        List<SysMenu> list = null;
        if (RoleId == 1) {//管理员角色
            LambdaQueryWrapper<SysMenu> qw = new LambdaQueryWrapper<>();
            qw.orderByDesc(SysMenu::getOrderno, SysMenu::getParentid);
            list = sysMenuMapper.selectList(qw);
        } else {//其他角色
            list = sysMenuMapper.selectAllMenuAuth(RoleId);
        }
        for (int i = 0; i < list.size(); i++) {
            String Auth = list.get(i).getAuth();
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, String>[] Actions = objectMapper.readValue(Auth, Map[].class);
            List<String> valueList = new ArrayList<>();
            for (int j = 0; j < Actions.length; j++) {
                valueList.add(Actions[j].get("value"));
            }

            Map<String, Object> map = new HashMap<>();
            map.put("id", list.get(i).getMenuId());
            map.put("name", list.get(i).getMenuname());
            map.put("url", list.get(i).getUrl());
            map.put("parentId", list.get(i).getParentid());
            map.put("icon", list.get(i).getIcon());
            map.put("enable", list.get(i).getEnable());
            map.put("tableName", list.get(i).getTablename());
            map.put("permission", valueList);
            listmap.add(map);
        }
        redisUtils.setKey("meunCache", listmap);
    }


}
